package client_gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import actions.AddUserAction;
import actions.UnblockUserAction;
import client_tree.User;

/**
 * Klasa predstavlja BlockList Dialog.
 * 
 * @author UrosKrkic & Radivoje Popovic
 */
public class BlockListDialog extends JDialog {

	/**
	 * Parent - prozor aplikacije.
	 */
	private ClientWindow owner;
	
	
	private JPanel jContentPane = null;


	private JPanel blockedInCLPanel = null;
	private JPanel blockedNotInCLPanel = null;
	private JButton unblockButton = null;
	private JScrollPane blockedInCLScrollPane = null;
	private JList blockedInCLList = null;
	private JLabel blockedInCLLabel = null;
	private JButton blockedNotInCLButton = null;
	private JScrollPane blockedNotInCLScrollPane = null;
	private JList blockedNotInCLList = null;
	private JLabel blockedNotInCLLabel = null;
	private JButton closeButton = null;
	
	
	// block liste
	private Vector<String> blockedInCLData;
	private Vector<String> blockedNotInCLData;
	
	
	// selektovani korisnici (username-ovi)
	private String selectedValueInCL = "";
	private String selectedValueNotInCL = "";
	

	
	/**
	 * Konstruktor.
	 * @param parent - prozor aplikacije
	 * @author UrosKrkic & Radivoje Popovic
	 */
	public BlockListDialog(ClientWindow parent) {
		super(parent, "Block List Dialog", true);
		
		this.owner = parent;

		initializeBlockLists();
		
		initialize();
	}
	
	
	/**
	 * Metod inicijalizuje block liste i popunjava ih odgovarajucim vrednostima.
	 * @author UrosKrkic & Radivoje Popovic
	 */
	private void initializeBlockLists() {
		blockedInCLData = new Vector<String>();
		blockedNotInCLData = new Vector<String>();
		
		Vector<String> tempList = new Vector<String>(this.owner.getApplicaton().getClientUser().getBlockList());
		
		for (int i = 0; i < tempList.size(); i++) {
			if ( this.owner.getApplicaton().getClientModel().getOnlineModel().getUserByUsername(tempList.get(i)) != null ||
				 this.owner.getApplicaton().getClientModel().getOfflineModel().getUserByUsername(tempList.get(i)) != null ) {
					blockedInCLData.add(tempList.get(i));
			}
			else {
					blockedNotInCLData.add(tempList.get(i));
			}
		}
	}
	
	
	
	/**
	 * Metod vrsi inicijalizaciju dialoga.
	 * @author UrosKrkic & Radivoje Popovic
	 */
	private void initialize() {
		this.setSize(400, 300);
		this.setLocation(owner.getX() + owner.getWidth() / 2 - this.getWidth() / 2, owner.getY() + owner.getHeight() / 2 - this.getHeight() / 2);
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		
		this.setIconImage(owner.getIconImage());
	}
	
	
	
	// Metode koje inicijalizuju komponente dialoga.
	
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			
			jContentPane.setLayout(null);

			jContentPane.add(getBlockedInCLPanel(), null);
			jContentPane.add(getBlockedNotInCLPanel(), null);
			jContentPane.add(getCloseButton(), null);
		}
		
		return jContentPane;
	}


	private JPanel getBlockedInCLPanel() {
		if (blockedInCLPanel == null) {
			blockedInCLLabel = new JLabel();
			blockedInCLLabel.setText("Included in Contact List:");
			blockedInCLPanel = new JPanel();
			blockedInCLPanel.setLayout(new BorderLayout());
			blockedInCLPanel.setBounds(new Rectangle(10, 15, 180, 210));
			
			
			blockedInCLPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED), "Blocked User", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			blockedInCLPanel.add(getUnblockButton(), BorderLayout.SOUTH);
			blockedInCLPanel.add(getBlockedInCLScrollPane(), BorderLayout.CENTER);
			blockedInCLPanel.add(blockedInCLLabel, BorderLayout.NORTH);
		}
		return blockedInCLPanel;
	}


	private JPanel getBlockedNotInCLPanel() {
		if (blockedNotInCLPanel == null) {
			blockedNotInCLLabel = new JLabel();
			blockedNotInCLLabel.setText("Not included in Contact List:");
			blockedNotInCLPanel = new JPanel();
			blockedNotInCLPanel.setLayout(new BorderLayout());
			blockedNotInCLPanel.setBounds(new Rectangle(205, 15, 180, 210));
			
			TitledBorder titledBorder = BorderFactory.createTitledBorder(BorderFactory.
					createEtchedBorder(EtchedBorder.RAISED),
					"Blocked User", javax.swing.border.TitledBorder.CENTER,
					javax.swing.border.TitledBorder.DEFAULT_POSITION,
					null, null);
			
			blockedNotInCLPanel.setBorder(titledBorder);
			blockedNotInCLPanel.add(getBlockedNotInCLButton(), BorderLayout.SOUTH);
			blockedNotInCLPanel.add(getBlockedNotInCLScrollPane(), BorderLayout.CENTER);
			blockedNotInCLPanel.add(blockedNotInCLLabel, BorderLayout.NORTH);
		}
		return blockedNotInCLPanel;
	}


	private JButton getUnblockButton() {
		if (unblockButton == null) {
			unblockButton = new JButton(new UnblockUserAction(owner));
			unblockButton.setText("Unblock");
			unblockButton.setPreferredSize(new Dimension(80, 25));
			unblockButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					blockedInCLData.remove(selectedValueInCL);
					SwingUtilities.updateComponentTreeUI(blockedInCLList);
				}
			});

		}
		return unblockButton;
	}


	private JScrollPane getBlockedInCLScrollPane() {
		if (blockedInCLScrollPane == null) {
			blockedInCLScrollPane = new JScrollPane();
			blockedInCLScrollPane.setViewportView(getBlockedInCLList());
		}
		return blockedInCLScrollPane;
	}


	private JList getBlockedInCLList() {
		if (blockedInCLList == null) {
			blockedInCLList = new JList(blockedInCLData);
			blockedInCLList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			blockedInCLList.addListSelectionListener(new ListSelectionListener() {
				public void valueChanged(ListSelectionEvent e) {
					selectedValueInCL = (String)blockedInCLList.getSelectedValue();
					
					User user = owner.getApplicaton().getClientModel().getOnlineModel().getUserByUsername(selectedValueInCL);
					if (user == null) {
						user = owner.getApplicaton().getClientModel().getOfflineModel().getUserByUsername(selectedValueInCL);
					}
					
					owner.getTree().setSelectedUser(user);
				}
			});
		}
		return blockedInCLList;
	}


	private JButton getBlockedNotInCLButton() {
		if (blockedNotInCLButton == null) {
			blockedNotInCLButton = new AddUserAction(owner, BlockListDialog.this);
			blockedNotInCLButton.setText("Add");
			blockedNotInCLButton.setPreferredSize(new Dimension(80, 25));
			blockedNotInCLButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					blockedNotInCLData.remove(selectedValueNotInCL);
					blockedInCLData.add(selectedValueNotInCL);
					SwingUtilities.updateComponentTreeUI(blockedNotInCLList);
					SwingUtilities.updateComponentTreeUI(blockedInCLList);
				}
			});
		}
		return blockedNotInCLButton;
	}


	private JScrollPane getBlockedNotInCLScrollPane() {
		if (blockedNotInCLScrollPane == null) {
			blockedNotInCLScrollPane = new JScrollPane();
			blockedNotInCLScrollPane.setViewportView(getBlockedNotInCLList());
		}
		return blockedNotInCLScrollPane;
	}


	private JList getBlockedNotInCLList() {
		if (blockedNotInCLList == null) {
			blockedNotInCLList = new JList(blockedNotInCLData);
			blockedNotInCLList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			blockedNotInCLList.addListSelectionListener(new ListSelectionListener() {
				public void valueChanged(ListSelectionEvent e) {
					selectedValueNotInCL = (String)blockedNotInCLList.getSelectedValue();
					System.out.println(selectedValueNotInCL);
				}
			});
		}
		return blockedNotInCLList;
	}


	private JButton getCloseButton() {
		if (closeButton == null) {
			closeButton = new JButton();
			closeButton.setBounds(new Rectangle(158, 240, 80, 25));
			closeButton.setPreferredSize(new Dimension(80, 25));
			closeButton.setText("Close");
			closeButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					BlockListDialog.this.dispose();
				}
			});
		}
		return closeButton;
	}

	
	
	/**
	 * Metod vraca referencu na selektovanog korisnika iz liste blokiranih korisnika koji
	 * nisu u kontakt listi.
	 * @return - selektovnai korisnik (username)
	 * @author UrosKrkic & Radivoje Popovic
	 */
	public String getSelectedValueNotInCL() {
		return this.selectedValueNotInCL;
	}
	
}
